package com.ruoyi.ctcx.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.domain.TbInsuranceMain;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCase;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCasePay;
import com.ruoyi.ctcx.domain.ro.InsuranceParam;
import com.ruoyi.ctcx.domain.ro.InsuranceQueryParam;
import com.ruoyi.ctcx.domain.vo.TbInsuranceSettleDetailVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceSettleQueryVo;
import com.ruoyi.ctcx.domain.vo.TbInsuranceSettleStatics;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCasePayService;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceSettle;
import com.ruoyi.ctcx.service.ITbInsuranceSettleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险理赔信息Controller
 * 
 * @author lss
 * @date 2024-11-19
 */
@RestController
@RequestMapping("/insurance/settle")
public class TbInsuranceSettleController extends BaseControllerExt
{
    @Autowired
    private ITbInsuranceSettleService tbInsuranceSettleService;

    @Autowired
    private ITbInsuranceSettleCaseService tbInsuranceSettleCaseService;

    @Autowired
    private ITbInsuranceSettleCasePayService tbInsuranceSettleCasePayService;

    /**
     * 查询保险理赔信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        startPage();
        List<TbInsuranceSettleQueryVo> list = tbInsuranceSettleService.getSettleList(insuranceParam);
        return getDataTable(list);
    }


    /**
     * 导出保险理赔信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:export')")
    @Log(title = "保险理赔信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsuranceParam insuranceParam)
    {
        initDeptId(insuranceParam);
        List<TbInsuranceSettleQueryVo> list = tbInsuranceSettleService.getSettleList(insuranceParam);
        ExcelUtil<TbInsuranceSettleQueryVo> util = new ExcelUtil<TbInsuranceSettleQueryVo>(TbInsuranceSettleQueryVo.class);
        util.exportExcel(response, list, "保险理赔信息数据");
    }

    /**
     * 获取保险理赔信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tbInsuranceSettleService.selectTbInsuranceSettleById(id));
    }


    @GetMapping("/getSettleDetail")
    public AjaxResult getContractDetail(InsuranceQueryParam insuranceQueryParam)
    {
        TbInsuranceSettleDetailVo tbInsuranceSettleDetailVo = tbInsuranceSettleService.selectTbInsuranceSettleByInsureId(insuranceQueryParam);
        if (null != tbInsuranceSettleDetailVo && null != tbInsuranceSettleDetailVo.getId())
        {
            TbInsuranceSettleCase query = new TbInsuranceSettleCase();
            query.setSettleId(tbInsuranceSettleDetailVo.getId());
            List<TbInsuranceSettleCase> caseList = tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseList(query);
            for (TbInsuranceSettleCase tbInsuranceSettleCase : caseList)
            {
                TbInsuranceSettleCasePay payQuery = new TbInsuranceSettleCasePay();
                payQuery.setCaseId(tbInsuranceSettleCase.getId());
                List<TbInsuranceSettleCasePay> payList = tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayList(payQuery);
                if (null != payList)
                {
                    tbInsuranceSettleCase.setPayList(payList);
                }
            }
            if (null != caseList)
            {
                tbInsuranceSettleDetailVo.setCaseList(caseList);
            }
        }
        return success(tbInsuranceSettleDetailVo);
    }

    /**
     * 新增保险理赔信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:add')")
    @Log(title = "保险理赔信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceSettle tbInsuranceSettle)
    {
        return toAjax(tbInsuranceSettleService.insertTbInsuranceSettle(tbInsuranceSettle));
    }

    /**
     * 修改保险理赔信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:edit')")
    @Log(title = "保险理赔信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceSettle tbInsuranceSettle)
    {
        return toAjax(tbInsuranceSettleService.updateTbInsuranceSettle(tbInsuranceSettle));
    }

    /**
     * 删除保险理赔信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:settle:remove')")
    @Log(title = "保险理赔信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbInsuranceSettleService.deleteTbInsuranceSettleByIds(ids));
    }
}
